public class Solution9 {
    //376. 摆动序列
    public int wiggleMaxLength(int[] nums) {
        int n = nums.length;
        int[] f = new int[n];  //以i结尾递增的
        int[] g = new int[n]; //以i结尾递减的
        for (int i = 0; i < n; i++) {
            f[i] = g[i] = 1;
        }
        int ret = 1;
        for (int i = 1; i < n; i++) {
            for (int j = i-1; j >= 0; j--) {
                if (nums[i] > nums[j]) {
                    f[i] = Math.max(g[j]+1,f[i]);
                } else if (nums[i] < nums[j]) {
                    g[i] = Math.max(f[j]+1,g[i]);
                }
            }
            ret = Math.max(ret,Math.max(f[i],g[i]));
        }
        return ret;
    }
}
